﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace app.Api
{
    #region 参数请求类
    public enum REQPARAMLEVEL
    {
        MUST_VALID,
        VALID,
        FREE
    }
    public class ReqParamOption<T>
    {
        public string Name = "";
        public REQPARAMLEVEL Level = REQPARAMLEVEL.MUST_VALID;
        public T DefaultValue;
        public T Result;
        public STATUS LackStatus = STATUS.LACK_PARAM;
        public string LackMessage = "缺少参数";
        public STATUS TypeErrorStatus = STATUS.TYPEERROR_PARAM;
        public string TypeErrorMessage = "参数类型错误";
        public STATUS InvalidStatus = STATUS.INVALID_PARAM;
        public string InvalidMessage = "无效的参数";
        public Func<T, bool> InvalidTest = (value) => { return true; };
    }
    public interface IReqParamOption
    {
        bool TypeTest(string value);
    }
    public class ReqStringParamOption : ReqParamOption<string>, IReqParamOption
    {
        public ReqStringParamOption()
        {
            DefaultValue = "";
        }

        public bool TypeTest(string value)
        {
            this.Result = value;
            return true;
        }
    }
    public class ReqObjectParamOption<T> : ReqParamOption<T>, IReqParamOption
    {
        public ReqObjectParamOption()
        {
            DefaultValue = default(T);
        }

        public bool TypeTest(string value)
        {
            try
            {
                Result = JsonConvert.DeserializeObject<T>(value);
                return true;
            }
            catch(Exception e)
            {
                TypeErrorMessage = e.Message;
                return false;
            }
        }
    }
    public class ReqBooleanParamOption : ReqParamOption<bool>, IReqParamOption
    {
        public ReqBooleanParamOption()
        {
            DefaultValue = false;
        }

        public bool TypeTest(string value)
        {
            if(value == "1" || value == "true")
            {
                this.Result = true;
                return true;
            }
            else if(value == "0" || value == "false")
            {
                this.Result = false;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    public class ReqIntParamOption : ReqParamOption<int>, IReqParamOption
    {
        public ReqIntParamOption()
        {
            DefaultValue = 0;
            TypeErrorStatus = STATUS.TYPEERROR_INT;
            TypeErrorMessage = "错误的INT类型";
        }

        public bool TypeTest(string value)
        {
            int temp;
            if (value == "True" || value == "true")
            {
                this.Result = 1;
                return true;
            }
            if (value == "False" || value == "false")
            {
                this.Result = 0;
                return true;
            }
            if (int.TryParse(value, out temp))
            {
                this.Result = temp;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    public class ReqDoubleParamOption : ReqParamOption<double>, IReqParamOption
    {
        public ReqDoubleParamOption()
        {
            DefaultValue = 0;
            TypeErrorStatus = STATUS.TYPEERROR_INT;
            TypeErrorMessage = "错误的double类型";
        }

        public bool TypeTest(string value)
        {
            double temp;
            if (double.TryParse(value, out temp))
            {
                this.Result = temp;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    public class ReqTimeStampParamOption : ReqParamOption<string>, IReqParamOption
    {
        public ReqTimeStampParamOption()
        {
            DefaultValue = "0000000000000";
            TypeErrorStatus = STATUS.TYPEERROR_TIMESTAMP;
            TypeErrorMessage = "错误的时间戳格式";
        }

        public bool TypeTest(string value)
        {
            // 还需要检测13个字符是否都为数字等，暂时没有判断
            if (value.Length == 13)
            {
                this.Result = value;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    #endregion
}